package com.hongxun.design.utils.soundUtil;

import android.media.MediaRecorder;
import java.io.IOException;
import com.hongxun.design.interfaces.IVoiceManager;
import com.hongxun.design.mytools.MyLog;


/**
 * Created by zerdoor_pc on 2016/4/21.
 */
public class URecorder implements IVoiceManager {

    private final String TAG = URecorder.class.getName();
    private String path;
    private MediaRecorder mRecorder;
    private static URecorder intence;
    public URecorder(String path) {
        this.path = path;
        mRecorder = new MediaRecorder();
    }
    public static URecorder getIntence(String path) {
        if (intence == null) {
            intence = new URecorder(path);
        }
        return intence;
    }
    /*
     * 开始录音
     * @return boolean
     */
    @Override
    public boolean start() {
        //设置音源为Micphone
        if (mRecorder==null) mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置封装格式
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(path);
        //设置编码格式
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            MyLog.L("prepare() failed");
        }
        //录音
        mRecorder.start();
        return false;
    }

    /*
     * 停止录音
     * @return boolean
     */
    @Override
    public boolean stop() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        return false;
    }
}
